package io.itit.ecp.admin.provide.helper;

import cn.hutool.core.date.DateTime;
import io.itit.ecp.admin.common.constants.EmissionFactorConstants;
import io.itit.ecp.admin.common.constants.ParamConstants;
import io.itit.ecp.admin.server.entity.ConvertStandardCoalCoefficientEntity;
import io.itit.ecp.admin.server.service.IConvertStandardCoalCoefficientService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

/*
 **********************
 * @Author tangdongming
 * @ClassName CarbonEmissionFactorHelper
 * @Description
 * @Date 2023/12/6 18:01
 **********************
 **/
@Component
@RequiredArgsConstructor
public class ConvertStandardCoalCoefficientHelper extends CarbonAbsFactorHelper {

    private static ConvertStandardCoalCoefficientHelper carbonEmissionFactorHelper;

    private final IConvertStandardCoalCoefficientService convertStandardCoalCoefficientService;

    public static ConvertStandardCoalCoefficientHelper i() {
        return carbonEmissionFactorHelper;
    }

    @Override
    public void afterPropertiesSet() {
        carbonEmissionFactorHelper = this;
    }

    @Override
    String getKeyPrefix() {
        return EmissionFactorConstants.CARBON_EMISSION_FACTOR_KEY;
    }

    @Override
    BigDecimal getFactor(String type, DateTime dateTime) {
        ConvertStandardCoalCoefficientEntity entity = convertStandardCoalCoefficientService
                .lambdaQuery()
                .le(ConvertStandardCoalCoefficientEntity::getSolidifyDate, dateTime)
                .eq(ConvertStandardCoalCoefficientEntity::getEnergyTypeId, type)
                .last(ParamConstants.LIMIT_ONE)
                .one();
        return entity == null ? null : entity.getCoefficient();
    }

}
